﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AceMobile.Models;

namespace AceMobile.Controllers.Admin
{
    [Authorize(Roles = "Administrator")]
    public class OrderManagerController : Controller
    {
        private MobileWebContext db = new MobileWebContext();

        //
        // GET: /OrderManager/

        public ViewResult Index()
        {
            var orderlists = db.OrderLists.Include(o => o.User);           
            return View(orderlists.ToList());
        }
        
        
        //
        // GET: /OrderManager/Edit/5
 
        public ActionResult Edit(int id)
        {
            OrderList orderlist = db.OrderLists.Find(id);
            ViewBag.UserID = new SelectList(db.Accounts, "ID", "UserName", orderlist.UserID);
            return View(orderlist);
        }

        //
        // POST: /OrderManager/Edit/5

        [HttpPost]
        public ActionResult Edit(OrderList orderlist)
        {
            if (ModelState.IsValid)
            {
                var order = db.OrderLists.Find(orderlist.ID);
                order.Status = orderlist.Status;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.UserID = new SelectList(db.Accounts, "ID", "UserName", orderlist.UserID);
            return View(orderlist);
        }

        

        [HttpPost]
        public ActionResult Delete(int id)
        {
            try
            {
                OrderList orderlist = db.OrderLists.Find(id);
                db.OrderLists.Remove(orderlist);
                db.SaveChanges();
                return Json(new { status = "Success" });
            }
            catch (Exception)
            {
                
            }
            return Json(new { status = "NotSuccessFull" });
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}